<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          索引器
        </h2>
      </div>
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      <div class="col-12 col-md-4 col-xl-3">
        <a class="card card-link card-link-pop" href="#" data-bs-toggle="modal" data-bs-target="#modal-jackett">
          <div class="card-cover card-cover-blurred text-center bg-black">
            <span class="avatar avatar-xl avatar-thumb avatar-rounded" style="background-image: url(../static/img/jackett.png)">
            </span>
          </div>
          <div class="card-body text-center">
            <div class="card-title mb-1">Jackett</div>
            <div class="text-muted">{% if Config.pt.search_indexer == "jackett" %}<span class="badge bg-green" title="已开启"></span> 正在使用{% endif %}</div>
          </div>
        </a>
      </div>
      <div class="col-12 col-md-4 col-xl-3">
        <a class="card card-link card-link-pop" href="#" data-bs-toggle="modal" data-bs-target="#modal-prowlarr">
          <div class="card-cover card-cover-blurred text-center bg-orange">
            <span class="avatar avatar-xl avatar-thumb avatar-rounded" style="background-image: url(../static/img/prowlarr.png)">
            </span>
          </div>
          <div class="card-body text-center">
            <div class="card-title mb-1">Prowlarr</div>
            <div class="text-muted">{% if Config.pt.search_indexer == "prowlarr" %}<span class="badge bg-green" title="已开启"></span> 正在使用{% endif %}</div>
          </div>
        </a>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-jackett" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title">Jackett</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-12">
              <div class="mb-3">
                <label class="form-label required">API Key <span class="form-help bg-red-lt" title="Jackett管理界面右上角复制API Key" data-bs-toggle="tooltip">?</span></label>
                <input type="text" value="{% if Config.jackett %}{{ Config.jackett.api_key or '' }}{% endif %}" id="jackett.api_key" class="form-control">
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="mb-3">
                <label class="form-label required">Torznab Feeds <span class="form-help bg-red-lt" title="Jackett管理界面Indexer中选择Copy Torznab Feed复制链接，一行配置一个地址，可配置多行" data-bs-toggle="tooltip">?</span></label>
                <textarea rows="5" id="jackett.indexers" class="form-control">{% if Config.jackett.indexers %}{{ Config.jackett.indexers|join('\n') }}{% endif %}</textarea>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <a href="javascript:test_indexer_config('jackett')" id="jackett_test_btn" class="btn me-auto">
          测试
        </a>
        <a href="javascript:save_indexer_config('jackett')" id="jackett_save_btn" class="btn btn-primary">确定</a>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-prowlarr" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title">Prowlarr</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">服务器地址 <span class="form-help bg-red-lt" title="Prowlarr访问地址，如为https需加https://前缀" data-bs-toggle="tooltip">?</span></label>
                <input type="text" value="{% if Config.prowlarr %}{{ Config.prowlarr.host or '' }}{% endif %}" id="prowlarr.host" class="form-control" placeholder="http://127.0.0.1:9696">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">API Key <span class="form-help bg-red-lt" title="在Prowlarr->Settings->General->Security-> API Key中获取" data-bs-toggle="tooltip">?</span></label>
                <input type="text" value="{% if Config.prowlarr %}{{ Config.prowlarr.api_key or '' }}{% endif %}" id="prowlarr.api_key" class="form-control">
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <a href="javascript:test_indexer_config('prowlarr')" id="prowlarr_test_btn" class="btn me-auto">
          测试
        </a>
        <a href="javascript:save_indexer_config('prowlarr')" id="prowlarr_save_btn" class="btn btn-primary">确定</a>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  // 当前处理的类型
  var currType = "";

  // 保存配置
  function save_config(type, func, test){
    currType = type;
    var params = {"test": test||false};
    $("#modal-"+ type +" input").each(function(){
      var key = $(this).attr("id");
      if($(this).attr("type")=="checkbox"){
        var value = $(this).prop("checked") ? true:false;
      }else{
        var value = $(this).val();
      }
      params[key] = value;
    });
    $("#modal-"+ type +" textarea").each(function(){
      var key = $(this).attr("id");
      var value = $(this).val();
      params[key] = value;
    });
    params['pt.search_indexer'] = type;
    ajax_post("update_config", params, func);
  }

  //保存配置、关闭和刷新页面
  function save_indexer_config(type){
    $("#modal-"+type).modal('hide');
    save_config(type, function (ret) {
      navmenu('indexer');
    });
  }

  //保存配置和测试配置
  function test_indexer_config(type){
    $("#" + type + "_test_btn").text("测试中...");
    save_config(type, function (ret) {
      if(currType == "jackett"){
        command = "pt.indexer.jackett|Jackett";
      }else if(currType == "prowlarr"){
        command = "pt.indexer.prowlarr|Prowlarr";
      }
      ajax_post("test_connection", {"command": command}, function(ret){
        if(ret.code == 0){
          $("#" + currType + "_test_btn").text("测试成功");
        }else{
          $("#" + currType + "_test_btn").text("测试失败！");
        }
      });
    }, true);
  }
</script>